package cn.vcjf.weapp.api.payment;

import cn.vcjf.weapp.api.goods.entity.GoodsOrder;
import cn.vcjf.weapp.api.payment.qfwxpay.service.AbstractQfWxPayAuth;
import cn.vcjf.weapp.api.payment.qfwxpay.service.AbstractQfCallBackService;
import cn.vcjf.weapp.common.util.SysParamUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 钱方微信支付
 * @Auther: zhengzihang
 * @Date: 2018/9/28 15:51
 * @Description:
 */
@Controller
@RequestMapping("/pay")
public class QfWxPayController {

	public static String QfWxPayController_URl = SysParamUtil.getParam("base.common.url")+ "/pay/doUnifiedOrder";
	public static String MERCH_NAME = SysParamUtil.getParam("APP_NAME");

	public static String getQfWxPayUrl(GoodsOrder goodsOrder) {
		String payType = goodsOrder.getPayType();
		return QfWxPayController.QfWxPayController_URl + "?orderNo=" + goodsOrder.getOrderNo() +
				"&goodsName=" + goodsOrder.getOrderName() +
				"&money=" + goodsOrder.getTraActualMoney() +
				"&merchName=" + MERCH_NAME +
				("minapp".equals(payType)?"":("&minapp=" + MERCH_NAME));
	}

	@Resource
	private AbstractQfWxPayAuth abstractQfWxPayAuth;
	@Autowired
	private AbstractQfCallBackService abstractQfCallBackService;


	@RequestMapping("/doUnifiedOrder")
	public void doUnifiedOrder(HttpServletRequest request, HttpServletResponse response) {
		try {
			String openId = request.getParameter("openId");
			if(StringUtils.isNotBlank(openId)){
				String queryStr = request.getQueryString();
				abstractQfWxPayAuth.doUnifiedOrder(response,openId , queryStr);
				return;
			}
			abstractQfWxPayAuth.doGet(request, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/wechat/callback")
	public void wxPayCallback(HttpServletRequest request, HttpServletResponse response) {
		try {
			abstractQfCallBackService.doCallBack(request,response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
